<?php
//自动生成,请根据需要修改
namespace app\topic\controller;

use app\common\controller\Base;
use think\Db;

/**
 * @title 评论页面（模块06-03）
 * @description 评论说明
 * @group 帖子（06）
 * @header name:model_id require:0 default:06-03 desc:模块
 * @groupremark 已使用错误码：1-06-03-0001 校验失败 1-06-03-0002 添加失败 1-06-03-0003 编辑校验失败 1-06-03-0004 编辑失败 1-06-03-0005 获取详情失败  1-06-03-0006 获取列表失败 1-06-03-0007获取单个详情失败 1-06-03-0008 删除失败
 */
class Comment extends Base
{
    //测试
    public function test()
    {
        return 'test 111';
    }

    /**
     * @title 添加评论
     * @description 添加评论
     * @author 李锐志
     * @url /topic/comment/add
     * @method *
     *
     * @header name:token require:1 default: desc:token
     *
     * @param name:topic_id type:int require:0 other: desc:帖子id
     * @param name:img_id type:int require:0 other: desc:评论图片id
     * @param name:user_id type:int require:0 other: desc:用户id
     * @param name:argument type:text require:0 other: desc:论点
     * @param name:saytext type:text require:0 other: desc:依据
     * @param name:reply_num type:int require:0 other: desc:回复数量
     * @param name:like_num type:int require:0 other: desc:点赞数量
     * @param name:img_url type:text require:0 other: desc:图片路径
     * @param name:create_time type:int require:0 other: desc:创建时间
     * @return data:评论@!
     * @data    id:id    topic_id:帖子id    img_id:评论图片id    user_id:用户id    argument:论点    saytext:依据    reply_num:回复数量    like_num:点赞数量    img_url:图片路径    create_time:创建时间
     * @return error_id:1-06-03-0001 校验失败  1-06-03-0002 添加失败
     */
    public function add()
    {
        $data = input();
        /**
         *$validate = new \app\topic\validate\Comment();
         *$validate->scene('add');
         *$result = $validate->batch(true)->check($data);
         *if ($result !== true) {
         *$msg['msg']='校验失败';
         *$msg['validate']=$validate->getError();
         *$msg['error_id']='1-06-03-0001';
         *return $this->errorJson($msg);
         *}
         **/
        $model = new \app\topic\model\Comment();
        $flag = $model->save($data);
        if ($flag) {
            $msg['msg'] = '添加成功';
            $msg['data'] = $model->get($model['id']);
            return $this->successJson($msg);
        } else {
            $msg['error_id'] = '1-06-03-0002';
            $msg['msg'] = '添加失败';
            return $this->errorJson($msg);
        }
    }

    /**
     * @title 编辑评论
     * @description 编辑评论
     * @author 李锐志
     * @url /topic/comment/edit
     * @method *
     *
     * @header name:token require:1 default: desc:token
     *
     * @param name:id type:int require:0 other: desc:id
     * @param name:topic_id type:int require:0 other: desc:帖子id
     * @param name:img_id type:int require:0 other: desc:评论图片id
     * @param name:user_id type:int require:0 other: desc:用户id
     * @param name:argument type:text require:0 other: desc:论点
     * @param name:saytext type:text require:0 other: desc:依据
     * @param name:reply_num type:int require:0 other: desc:回复数量
     * @param name:like_num type:int require:0 other: desc:点赞数量
     * @param name:img_url type:text require:0 other: desc:图片路径
     * @param name:create_time type:int require:0 other: desc:创建时间
     * @return data:评论@!
     * @data    id:id    topic_id:帖子id    img_id:评论图片id    user_id:用户id    argument:论点    saytext:依据    reply_num:回复数量    like_num:点赞数量    img_url:图片路径    create_time:创建时间
     * @return error_id:1-06-03-0003 校验失败 1-06-03-0004 修改失败
     */
    public function edit()
    {
        $data = input();
        $validate = new \app\topic\validate\Comment();
        $validate->scene('edit');
        $result = $validate->batch(true)->check($data);
        if ($result !== true) {
            $msg['msg'] = '校验失败';
            $msg['validate'] = $validate->getError();
            $msg['error_id'] = '1-06-03-0003';
            return $this->errorJson($msg);
        }
        if(gettype($data['img_url']) == 'string'){
            $data['img_url'] = trim($data['img_url'],',');
        }
        $model = new \app\topic\model\Comment();
        $urlu = null;
        $bus = null;
        if(!empty($data['img'])){
            $imgs = $model->where('id',$data['id'])->field('id,img_url')->select();
            foreach ($imgs as $k => $v){
                if($v['img_url'] != null){
                    $urlu = explode(",", $v['img_url']);
                    foreach ($urlu as $kk => $vv){
                        if(gettype($data['img']) == 'string'){
                            $data['img'] = trim($data['img'],',');
                            $urls = explode(",", $data['img']);
                            foreach ($urls as $kkk => $vvv){
                                if($vv == $vvv){
                                    $bus[] = $urlu[$kk];
                                }
                            }
                        }else{
                            $data['img'] = trim($data['img'],',');
                            if($vv == $data['img']){
                                unset($urlu[$kk]);
                            }
                        }
                    }
                }
            }
        }
        if($bus != null ){
            if(gettype($data['img_url']) == 'string'){
                $data['img_url'] = trim($data['img_url'],',');
                $data['img_url'] = explode(",", $data['img_url']);
            }

            foreach ($data['img_url'] as $k => $v){
                foreach ($bus as $kk => $vv){
                    if($v == $vv){
                        unset($data['img_url'][$k]);
                    }
                }
            }
            $data['img_url'] = implode(",", $data['img_url']);
        }else{
            if(!empty($data['img'])){
                $data['img'] = trim($data['img'],',');
                $urls = explode(",", $data['img']);
            }else{
                $urls = [];
            }


            if(gettype($data['img_url']) == 'string'){
                $data['imgs'] = trim($data['img_url'],',');
                $uimg = explode(",", $data['imgs']);
            }else{
                $uimg = $data['img_url'];
            }
            foreach ($urls as $k => $v){
                foreach ($uimg as $kk => $vv){
                    if($v == $vv){
                        unset($uimg[$kk]);
                        $data['img_url'] = implode(",", $uimg);
                    }
                }
            }
        }

        $flag = $model->edit($data);
        if ($flag !== false) {
            $msg['msg'] = '修改成功';
            $msg['data'] = $model->get($data['id']);
            return $this->successJson($msg);
        } else {
            $msg['error_id'] = '1-06-03-0004';
            $msg['msg'] = '修改失败';
            return $this->errorJson($msg);
        }
    }

    /**
     * @title 删除评论
     * @description 删除评论
     * @author 李锐志
     * @url /topic/comment/delete
     * @method *
     *
     * @param name:token require:1 default: desc:token
     *
     * @param name:id type:int|array require:1 other: desc:要删除的id
     * @return error_id:1-06-03-0008 删除失败
     */
    public function delete()
    {
        $id = input('id');
        $model = new \app\topic\model\Comment();
        $flag = $model->destroy($id);
        if ($flag) {
            $msg['msg'] = '删除成功';
            return $this->successJson($msg);
        } else {
            $msg['msg'] = '删除失败';
            $msg['error_id'] = '1-06-03-0008';
            return $this->errorJson($msg);
        }
    }

    /**
     * @title 单个查询评论
     * @description 单个查询评论
     * @author 李锐志
     * @url /topic/comment/info
     * @method *
     *
     * @param name:token require:1 default: desc:token
     *
     * @param name:id type:int require:0 other: desc:id
     * @param name:topic_id type:int require:0 other: desc:帖子id
     * @param name:img_id type:int require:0 other: desc:评论图片id
     * @param name:user_id type:int require:0 other: desc:用户id
     * @param name:argument type:text require:0 other: desc:论点
     * @param name:saytext type:text require:0 other: desc:依据
     * @param name:reply_num type:int require:0 other: desc:回复数量
     * @param name:like_num type:int require:0 other: desc:点赞数量
     * @param name:img_url type:text require:0 other: desc:图片路径
     * @param name:create_time type:int require:0 other: desc:创建时间
     * @return data:评论@!
     * @data    id:id    topic_id:帖子id    img_id:评论图片id    user_id:用户id    argument:论点    saytext:依据    reply_num:回复数量    like_num:点赞数量    img_url:图片路径    create_time:创建时间
     * @return error_id:1-06-03-0005 校验失败 1-06-03-0007 获取失败
     */
    public function info()
    {
        $model = new \app\topic\model\Comment();
        $Fields = $model->getTableFields();
        $data = \think\facade\Request::only($Fields);
        /**
         *$validate = new \app\topic\validate\Comment();
         *$validate->scene('info');
         *$result = $validate->batch(true)->check($data);
         */
        if (!$data) {
            $msg['msg'] = '必须有参数';
            $msg['error_id'] = '1-06-03-0005';
            return $this->errorJson($msg);
        }
        $model = new \app\topic\model\Comment();
        $info = $model->where($data)->find();
        if ($info) {
            $msg['msg'] = '获取成功';
            $msg['data'] = $info;
            return $this->successJson($msg);
        } else {

            $msg['msg'] = '获取失败';
            $msg['error_id'] = '1-06-03-0007';
            return $this->errorJson($msg);
        }
    }

    /**
     * @title 查询列表评论
     * @description 查询列表评论
     * @author 宋晓文
     * @url /topic/comment/get_list
     * @method *
     * @param name:token require:1 default: desc:token
     * @param name:page require:1 default: desc:第几页
     * @param name:pagesize require:1 default: desc:每页数
     * @return list:评论@
     * @list    id:id    topic_id:帖子id    img_id:评论图片id    user_id:用户id    argument:论点    saytext:依据    reply_num:回复数量    like_num:点赞数量    img_url:图片路径    create_time:创建时间
     * @return error_id:1-06-03-0006 校验失败
     */
    public function get_list()
    {
        $message = input();

        $model = new \app\topic\model\Comment();
        $Fields = $model->getTableFields();
        $data = \think\facade\Request::only($Fields);

        $where = array();
        foreach($data as $k=>$v){
            if($v===0){
                continue;
            }
            if ($v == null){
                unset($data[$k]);
            }
        }
        foreach ($data as $k => $v){
            if ($k === 'create_time') {
                array_push($where, [$k, '>=', ceil($data[$k][0] / 1000)]);
                array_push($where, [$k, '<=', ceil($data[$k][1] / 1000)]);
                continue;
            }
            if ($k === 'id' || $k === 'topic_id' || $k === 'comment_img_id' || $k === 'user_id' || $k === 'reply_num' || $k === 'like_num'){
                array_push($where,[$k,'=',$data[$k]]);
                continue;
            }
            if (isset($data[$k]) && $data[$k] && !empty($data[$k])){
                array_push($where,[$k,'like',"%{$data[$k]}%"]);
            }
        }

        $list = $model->where($where)->order('id','desc')->paginate();

        foreach ($list as $k=>$v){
            //处理图片
            if($v['img_url'] != null){
                $list[$k]['img_url'] = explode(",", $v['img_url']);
            }
            $topic_id = $v['topic_id'];
            $user_id = $v['user_id'];

            $topic = Db::name('topic')->where('id',$topic_id)->find();
            $user= get_curl('user','/user/info',['token'=>$message['token'],'id'=>$user_id]);
            if ($user['status']!=false){
                $list[$k]['nickname']=$user['data']['nickname'];
            }else{
                $list[$k]['nickname']="";
            }
            $list[$k]['topic_title']=$topic['title'];
        }

        $msg['list'] = $list;
        return $this->successJson($msg);
    }
}
